// LeanCloud 初始化訂單數據腳本

// 引入必要的模組
const AV = require('leancloud-storage');

// 初始化 LeanCloud
AV.init({
  appId: 'CL7w1kSS0w7j9BDiLaYSp7Ij-gzGzoHsz',
  appKey: 'vyxioicmtGv0X8Cz7yYEIoMA',
  serverURL: 'https://cl7w1kss.lc-cn-n1-shared.com'
});

// 模擬訂單數據
const orders = [
  {
    orderNo: 'P2035091978689',
    totalAmount: 56.90,
    payAmount: 56.90,
    discountAmount: 0,
    status: 6, // 交易關閉
    payStatus: 1, // 已付款
    payType: 1, // 假設是微信支付
    payTime: new Date('2035-09-19 9:39:39'),
    shipStatus: 0,
    receiverName: '周杰伦',
    receiverPhone: '13800138000',
    receiverAddress: '天朝北路1号楼',
    receiverRegion: '北京市/北京市/朝阳区/建外街道',
    remark: '',
    sellerRemark: '',
    refundStatus: 0, // 無售後
    channel: '抖音直播',
    items: [
      {
        productName: '飘柔洗护香波',
        productImg: '/public/OIP-C.webp',
        price: 56.90,
        quantity: 1,
        skuId: '2468896545',
        specifications: '默认规格'
      }
    ]
  },
  {
    orderNo: 'P2035091978690',
    totalAmount: 128.50,
    payAmount: 128.50,
    discountAmount: 10,
    status: 2, // 待發貨
    payStatus: 1, // 已付款
    payType: 1,
    payTime: new Date('2035-09-18 15:25:30'),
    shipStatus: 0,
    receiverName: '林俊杰',
    receiverPhone: '13900139000',
    receiverAddress: '演唱会路2号',
    receiverRegion: '上海市/上海市/徐汇区/湖南路街道',
    remark: '请确保新鲜',
    sellerRemark: '',
    refundStatus: 0,
    channel: '官网',
    items: [
      {
        productName: '有机蔬菜礼盒',
        productImg: '/public/OIP-C.webp',
        price: 128.50,
        quantity: 1,
        skuId: '2468896546',
        specifications: '精品装'
      }
    ]
  },
  {
    orderNo: 'P2035091978691',
    totalAmount: 269.70,
    payAmount: 269.70,
    discountAmount: 0,
    status: 3, // 部分發貨
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-19 9:39:39'),
    shipStatus: 1, // 已發貨
    shipTime: new Date('2035-09-20 14:30:00'),
    receiverName: '周杰伦',
    receiverPhone: '13800138000',
    receiverAddress: '天朝北路1号楼',
    receiverRegion: '北京市/北京市/朝阳区/建外街道',
    remark: '用户需要锅碗炉盘',
    sellerRemark: '',
    refundStatus: 0,
    channel: '微信小店',
    items: [
      {
        productName: '格兰仕烤箱12L 自动',
        productImg: '/public/OIP-C.webp',
        price: 69.90,
        quantity: 2,
        skuId: '2468896545',
        specifications: '默认规格'
      },
      {
        productName: '索迪斯手持除螨仪',
        productImg: '/public/OIP-C.webp',
        price: 129.90,
        quantity: 1,
        skuId: '2468896617',
        specifications: '默认规格'
      }
    ],
    shippedCount: 1,
    totalCount: 2
  },
  {
    orderNo: 'P2035091978692',
    totalAmount: 23.80,
    payAmount: 23.80,
    discountAmount: 0,
    status: 4, // 待收貨
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-19 9:39:39'),
    shipStatus: 1,
    shipTime: new Date('2035-09-21 10:15:00'),
    receiverName: '周杰伦',
    receiverPhone: '13800138000',
    receiverAddress: '天朝北路1号楼',
    receiverRegion: '北京市/北京市/朝阳区/建外街道',
    remark: '',
    sellerRemark: '',
    refundStatus: 0,
    channel: '小红书订单',
    items: [
      {
        productName: '三十九度超鲜 国产贵妃芒',
        productImg: '/public/OIP-C.webp',
        price: 23.80,
        quantity: 1,
        skuId: '2468896545',
        specifications: '默认规格'
      }
    ],
    shippedCount: 1,
    totalCount: 1
  },
  {
    orderNo: 'P2035091978693',
    totalAmount: 56.90,
    payAmount: 56.90,
    discountAmount: 0,
    status: 5, // 交易完成
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-19 9:39:39'),
    shipStatus: 1,
    shipTime: new Date('2035-09-22 9:00:00'),
    receiveTime: new Date('2035-09-24 16:30:00'),
    receiverName: '周杰伦',
    receiverPhone: '13800138000',
    receiverAddress: '天朝北路1号楼',
    receiverRegion: '北京市/北京市/朝阳区/建外街道',
    remark: '',
    sellerRemark: '',
    refundStatus: 0,
    channel: '腾讯惠聚',
    items: [
      {
        productName: '正大食品（CP）鸡翅中',
        productImg: '/public/OIP-C.webp',
        price: 56.90,
        quantity: 1,
        skuId: '2468896545',
        specifications: '默认规格'
      }
    ],
    shippedCount: 1,
    totalCount: 1
  },
  {
    orderNo: 'P2035091978694',
    totalAmount: 56.90,
    payAmount: 56.90,
    discountAmount: 0,
    status: 6, // 交易關閉
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-19 9:39:39'),
    shipStatus: 0,
    receiverName: '周杰伦',
    receiverPhone: '13800138000',
    receiverAddress: '天朝北路1号楼',
    receiverRegion: '北京市/北京市/朝阳区/建外街道',
    remark: '',
    sellerRemark: '',
    refundStatus: 0,
    channel: '抖音直播',
    items: [
      {
        productName: '飘柔洗护香波',
        productImg: '/public/OIP-C.webp',
        price: 56.90,
        quantity: 1,
        skuId: '2468896545',
        specifications: '默认规格'
      }
    ]
  },
  // 新增售後狀態：售后中(refundStatus=1)的訂單
  {
    orderNo: 'P2035091978695',
    totalAmount: 199.00,
    payAmount: 199.00,
    discountAmount: 0,
    status: 4, // 待收貨
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-19 11:30:00'),
    shipStatus: 1,
    shipTime: new Date('2035-09-21 09:00:00'),
    receiverName: '张学友',
    receiverPhone: '13700137000',
    receiverAddress: '星光大道88号',
    receiverRegion: '广东省/广州市/天河区/五山街道',
    remark: '需要发票',
    sellerRemark: '',
    refundStatus: 1, // 售后中
    channel: '支付宝商家小程序',
    items: [
      {
        productName: 'HUAWEI FreeBuds 5i 真无线蓝牙耳机',
        productImg: '/public/OIP-C.webp',
        price: '¥199.00',
        quantity: 1,
        skuId: '8847563210',
        specifications: '白色'
      }
    ],
    shippedCount: 1,
    totalCount: 1
  },
  // 新增售後狀態：待商家处理(refundStatus=2)的訂單
  {
    orderNo: 'P2035091978696',
    totalAmount: 1299.00,
    payAmount: 1299.00,
    discountAmount: 0,
    status: 4, // 待收貨
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-18 14:20:00'),
    shipStatus: 1,
    shipTime: new Date('2035-09-20 11:30:00'),
    receiverName: '刘德华',
    receiverPhone: '13600136000',
    receiverAddress: '华星路99号',
    receiverRegion: '广东省/深圳市/南山区/科技园街道',
    remark: '',
    sellerRemark: '用户申请退货，质量问题',
    refundStatus: 2, // 待商家处理
    channel: '微信小店',
    items: [
      {
        productName: 'OPPO Reno10 Pro 5G手机',
        productImg: '/public/OIP-C.webp',
        price: '¥1299.00',
        quantity: 1,
        skuId: '7758258966',
        specifications: '星空黑 8+256GB'
      }
    ],
    shippedCount: 1,
    totalCount: 1
  },
  // 新增售後狀態：售后关闭(refundStatus=3)的訂單
  {
    orderNo: 'P2035091978697',
    totalAmount: 79.90,
    payAmount: 79.90,
    discountAmount: 0,
    status: 5, // 交易完成
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-17 10:15:00'),
    shipStatus: 1,
    shipTime: new Date('2035-09-18 16:30:00'),
    receiveTime: new Date('2035-09-20 14:20:00'),
    receiverName: '黎明',
    receiverPhone: '13500135000',
    receiverAddress: '东方路123号',
    receiverRegion: '上海市/上海市/浦东新区/陆家嘴街道',
    remark: '',
    sellerRemark: '用户申请退货后取消申请',
    refundStatus: 3, // 售后关闭
    channel: '腾讯惠聚',
    items: [
      {
        productName: '飞科剃须刀FS372',
        productImg: '/public/OIP-C.webp',
        price: '¥79.90',
        quantity: 1,
        skuId: '6655441122',
        specifications: '标准版'
      }
    ],
    shippedCount: 1,
    totalCount: 1
  },
  // 新增售後狀態：退款成功(refundStatus=4)的訂單
  {
    orderNo: 'P2035091978698',
    totalAmount: 89.50,
    payAmount: 89.50,
    discountAmount: 10.00,
    status: 6, // 交易关闭
    payStatus: 1,
    payType: 1,
    payTime: new Date('2035-09-16 09:25:00'),
    shipStatus: 0,
    receiverName: '郭富城',
    receiverPhone: '13400134000',
    receiverAddress: '舞王路66号',
    receiverRegion: '香港特别行政区/香港岛/中西区',
    remark: '急需使用',
    sellerRemark: '用户申请退款，商品缺货',
    refundStatus: 4, // 退款成功
    channel: '支付宝商家小程序',
    items: [
      {
        productName: '得力订书机办公用品',
        productImg: '/public/OIP-C.webp',
        price: '¥89.50',
        quantity: 1,
        skuId: '3344556677',
        specifications: '金属款'
      }
    ],
    shippedCount: 0,
    totalCount: 1
  }
];

// 保存訂單到 LeanCloud
async function saveOrders() {
  try {
    console.log('開始保存訂單數據...');
    
    // 清除之前的訂單數據（可選）
    const query = new AV.Query('Order');
    const orderCount = await query.count();
    
    if (orderCount > 0) {
      console.log(`找到 ${orderCount} 筆舊訂單數據，準備刪除...`);
      const oldOrders = await query.find();
      await AV.Object.destroyAll(oldOrders);
      console.log('舊訂單數據已刪除');
    }
    
    // 保存新訂單數據
    for (const orderData of orders) {
      const Order = AV.Object.extend('Order');
      const order = new Order();
      
      // 設置訂單基本信息
      for (const [key, value] of Object.entries(orderData)) {
        if (key !== 'items') {
          if (value instanceof Date) {
            order.set(key, { '__type': 'Date', 'iso': value.toISOString() });
          } else {
            order.set(key, value);
          }
        }
      }
      
      // 保存訂單
      await order.save();
      console.log(`訂單 ${orderData.orderNo} 已保存`);
    }
    
    console.log('所有訂單數據已成功保存！');
  } catch (error) {
    console.error('保存訂單數據失敗:', error);
  }
}

// 執行保存操作
saveOrders(); 